package com.example.wanted.controller;

import com.example.wanted.model.User;
import com.example.wanted.service.UserService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class UserController {

    final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }
   // @PostMapping("/user")
//    @RequestMapping(value = "/user",method = RequestMethod.POST,consumes = "application/json")
//    public void saveUser(@RequestBody User user){
//        System.out.println("start");
////        System.out.println(user);
//        //檢查用戶名是否被使用
////        if(userService.checkNameUsed(user.getUser_name())){
////            return "该用戶名已被使用";
////        }else {
////            userService.save(user);
////            //System.out.println(user);
////            return "注冊成功";
////        }
//
//    }
    @PostMapping("user")
    public void saveUser(@RequestBody User user, HttpServletResponse res, HttpServletRequest req){
        System.out.println(user);
        if(userService.canfindByOpenid(user.getOpenid())){
            System.out.println("已有此用戶");
        }else {
            userService.save(user);
        }

    }

    @GetMapping("get_user_by_openid")
    public User getUser(@RequestParam String openid, HttpServletResponse res, HttpServletRequest req){
        System.out.println(openid);
        return userService.findByOpenid(openid);
    }


//    @RequestMapping(value = "/vertification",method = RequestMethod.POST,consumes = "application/json")
//    public String checkUser(@RequestBody User user){
//
//        //檢查用戶密碼是否正确
//        if(userService.checkNameUsed(user.getUser_name())){
//            if(userService.checkPasswordUsed(user)){
//                return "登入成功";
//            }else{
//                return "密码不正确，请重试";
//            }
//        }else {
//            return "此用户名尚未注册";
//        }
//
//
//    }
}
